require([
  'jquery',
  'pageInit',
  'dataTable',
  'loading',
  'bootstrap-dialog',
  'notification',
  'listAsyncOp'
], function($, vamakerPage, VamDataTable, VamLoader, BootstrapDialog, VamOpMsg, VamListOp) {
  $(function() {
    var 
      $dataTable = $('#data-table'),
      dataUrl = VAM.getVar('DATA_LIST_URL'),
      dataEditOp = VAM.getVar('DATA_EDIT_URL'),
      dataDeleteOp = VAM.getVar('LIST_DELETE_OP_URL'),
      $reportDataThead = $dataTable.find('thead'),
      $loadingWrap = new VamLoader().loader($('#list-loading-wrap')),
    listTable, tableOptions = {};
    // Table settings.
    tableOptions = {
      'ajax': {
        'url': dataUrl
      },
      'columns': [
        {'data': 'id'},
        {'data': 'name'},
        {'data': 'type'},
        {'data': 'domain'},
        {'data': 'updateDate'},
        {'data': 'operate'}
      ],
      'columnDefs': [
        {
          'targets': [0,1,4],
          'render': function (data, type, full, meta) {
            return '<p title="' + data + '">' + data +'</p>';
          }
        },
        {
          'targets': 2,
          'render': function (data, type, full, meta) {
            var result = '';
            if (data === 'black') {
              result = '<p title="' + VAM.j('blacklist') + '">' + VAM.j('blacklist') + '</p>';
            } else if (data === 'white') {
              result = '<p title="' + VAM.j('whitelist') + '">' + VAM.j('whitelist') + '</p>';
            };
            return result;
          }
        },
        {
          'targets': 3,
          'render': function (data, type, full, meta) {
            var result = '';
            for (var i = 0,len = data.length; i < len; i++) {
              result += '<p class="text-primary" title="' + data[i] + '">' + data[i] + '</p>';
            };
            return result;
          }
        },
        {
          'targets': -1,
          'render': function (data, type, full, meta) {
            return '<div class="tab-btn-group">'
                  + '<input class="row-info" type="hidden" value="' + full.id + '" >'
                  + '<a href="' + dataEditOp + '?id=' + full.id + '" class="btn btn-primary btn-xs tab-edit" data-type="edit" title="' + VAM.j('Edit') + '"><i class="fa fa-edit"></i></a>'
                  + '<button class="btn btn-danger btn-xs tab-delete" data-loading-text="<i class=\'fa fa-spinner fa-spin\'></i>" data-type="delete" title="' + VAM.j('Delete') + '"><i class="fa fa-trash"></i></button>'
                  + '</div>';
          }
        }
      ]
    };
    $dataTable
    .on('preXhr.dt', function(evt) {
      $loadingWrap.show();
    });
    listTable = new VamDataTable().dataTables($dataTable, tableOptions);
    // After table data list.
    listTable.on('xhr.dt', function(evt) {
      $loadingWrap.fadeOut();
    });

    $dataTable.on('click', 'tbody td .tab-delete', function(evt) {
      var $me = $(this),
        $rowInfo = $me.siblings('.row-info'),
        opId = $rowInfo.val(),
        opType = $me.data('type'),
        opData;
      evt.preventDefault();
      opData = {
        id: opId,
        type: opType,
        rnd: (new Date()).getTime()
      };
      BootstrapDialog.confirm({
        title: '删除名单',
        message: '确认删除该名单？',
        type: BootstrapDialog.TYPE_DANGER,
        closable: true, 
        draggable: true,
        btnCancelLabel: '取消', 
        btnOKLabel: '确定', 
        btnOKClass: 'btn-danger', 
        callback: function(result) {
          if(result) {
            VamListOp.listOp({
              url: dataDeleteOp,
              data: opData,
              before: function () {
                $me.button('loading');
              },
              success: function (data) {
                if (data.status === 'success') {
                  VamOpMsg.messager(data);
                  listTable.ajax.reload();
                } else {
                  VamOpMsg.messager(data);
                }
              },
              always: function () {
                $me.button('reset');
              }
            });
          }else {
          }
        }
      });
    });
  });
});
